//
//  TitleButton.swift
//  weibo
//
//  Created by chenhongen on 15/10/20.
//  Copyright © 2015年 陈弘根. All rights reserved.
//

import UIKit

class TitleButton: UIButton {
    
    /*
    override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
    return CGRectZero
    }
    override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
    return CGRectZero
    }
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Selected)
        setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        // 取消高亮状态
        adjustsImageWhenHighlighted = false
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setTitle(title: String?, forState state: UIControlState) {
        if let t = title{
            super.setTitle(t + "  ", forState: state)
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // 调整布局
        // 注意: 以下代码在OC中是不对的, 但是在Swift中是可以的
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = titleLabel!.frame.size.width
    }
}